﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for answers data manipulation.
    /// </summary>
    public class AnswersRepository : BaseRepository<Answer>, IAnswersRepository
    {
        #region Fields
        private IAnswersVotesRepository _answersVotesRepository; 
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="AnswersRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public AnswersRepository(IDataServiceContext<Answer> dataServiceContext, IAnswersVotesRepository answersVotesRepository)
            :base(dataServiceContext)
        {
            _answersVotesRepository = answersVotesRepository;
        }

        public override void Delete(Answer entity)
        {
            base.Delete(entity);

            _answersVotesRepository.DeleteAllAnswerVotes(entity.RowKey);
        }

        /// <summary>
        /// Gets the answers by question.
        /// </summary>
        /// <param name="questionID">The question ID.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>The answers of a question</returns>
        public List<Answer> GetAnswersByQuestion(string questionID, int page, int pageSize)
        {
            return GetAnswersByQuestion(questionID).Skip(page * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// Gets the answers by question.
        /// </summary>
        /// <param name="questionID">The question ID.</param>
        /// <returns>The answers of a question</returns>
        public List<Answer> GetAnswersByQuestion(string questionID)
        {
            return (from answer in Entities
                    where answer.Question == questionID
                    select answer).ToList();
        }

        /// <summary>
        /// Gets all user answers.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public List<Answer> GetAnswersByUser(string userID)
        {
            var answers = (from answer in Entities
                           where answer.Author == userID
                           select answer).ToList();

            return answers;
        }

        /// <summary>
        /// Deletes all user answers.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllUserAnswers(string userID)
        {
            var answers = GetAnswersByUser(userID);

            answers.ForEach(answer => Delete(answer));
        }

        /// <summary>
        /// Deletes all question answers.
        /// </summary>
        /// <param name="questionID">The question ID.</param>
        public void DeleteAllQuestionAnswers(string questionID)
        {
            var answers = GetAnswersByQuestion(questionID);

            answers.ForEach(answer => Delete(answer));
        }
    }
}
